package com.yyy.instagram.common.CropImageView;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.AnimationSet;
import android.widget.ImageView;

/**
 * Created by qqqqqqq on 2016/10/14.
 */

public class CropImageView extends ImageView {
    float oldX = -1;
    float oldY = -1;

    float translateX = -1;
    float translateY = -1;
    float allTranslateX = 0;
    float allTranslateY = 0;
    public CropImageView(Context context) {
        super(context);
        init();
    }

    public CropImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CropImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){

    }

    @Override
    protected void onDraw(Canvas canvas) {

        if(translateX != -1 || translateY != -1){
            canvas.translate(translateX,translateY);
        }
        super.onDraw(canvas);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:{
                oldX = event.getX();
                oldY = event.getY();
                return true;
            }
            case MotionEvent.ACTION_MOVE:{
                float x =  event.getX();
                float y =  event.getY();
                translate(x-oldX,y-oldY);
//                double distance = Math.sqrt(Math.pow(Math.abs(x-oldX),2)+Math.pow(Math.abs(x-oldY),2));

                break;
            }
            case MotionEvent.ACTION_UP:{
                System.out.println("++++++key_up");
//                oldX = -1;
//                oldY = -1;
//                translateX = -1;
//                translateY = -1;
                backOrigiPosion();

                break;
            }
            case MotionEvent.ACTION_CANCEL:{
                System.out.println("++++++key_cancel");
            }
        }
        return super.onTouchEvent(event);
    }

    public void translate(float x,float y){
        translateX = x;
        translateY = y;
        allTranslateX += x;
        allTranslateY +=y;
        invalidate();
    }
    public void backOrigiPosion(){
        ValueAnimator animatorx = ValueAnimator.ofFloat(translateX,0);
        animatorx.setDuration(500);
        animatorx.setInterpolator(new LinearOutSlowInInterpolator());
        animatorx.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                translateX = (float) animation.getAnimatedValue();
                invalidate();
            }

        });
        ValueAnimator animatory = ValueAnimator.ofFloat(translateY,0);
        animatory.setDuration(500);
        animatory.setInterpolator(new LinearOutSlowInInterpolator());
        animatory.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                translateY = (float) animation.getAnimatedValue();
                invalidate();
            }

        });



        AnimatorSet set = new AnimatorSet();
        set.playTogether(animatorx,animatory);
        set.start();


    }
}
